using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Plugin.MassSend;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.UserContrl;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace MicroMsg.Manager
{
	public class MassMsgMgr
	{
		public class MassMsgContext
		{
			public MMObservableCollection<MassMsg, MassSendListItem> massMsgList;
			public int nLeftMsgTotal;
			public List<MassMsg> toExpandList;
			public MessagesListPage msgListPage;
		}
		private const string TAG = "MassMsgMgr";
		private const int msgPerReadAmount = 4;
		private const int msgReadAmount = 4;
		private static bool bRegisterMassMsgNotify = false;
		private static MassMsgMgr.MassMsgContext MassMsgInfo;
		public MassMsgMgr()
		{
			ChatMsgMgr.registerCleanMsg(new Action(MassMsgMgr.MassMsgClearAll));
		}
		public static ObservableCollection<MassMsg> getList()
		{
			List<MassMsg> list = StorageMgr.massMsg.getList();
			if (list == null)
			{
				return null;
			}
			return new ObservableCollection<MassMsg>(list);
		}
		public static void deleteMassMsg(MassMsg massMsg_0)
		{
			if (massMsg_0 == null)
			{
				return;
			}
			StorageMgr.massMsg.method_4(massMsg_0);
			MassMsgMgr.deleteMsgData(massMsg_0);
		}
		public static void deleteMsgData(MassMsg massMsg_0)
		{
			if (massMsg_0 == null)
			{
				return;
			}
			if (massMsg_0.IsImage())
			{
				StorageIO.deleteFile(massMsg_0.strFileName);
				StorageIO.deleteFile(massMsg_0.strThumbFileName);
				return;
			}
			if (massMsg_0.IsVoice())
			{
				StorageIO.deleteFile(massMsg_0.strFileName);
				return;
			}
			if (massMsg_0.IsVideo())
			{
				StorageIO.deleteFile(massMsg_0.strFileName);
				StorageIO.deleteFile(massMsg_0.strThumbFileName);
			}
		}
		private static void InitMassMsgWatcher()
		{
			if (!MassMsgMgr.bRegisterMassMsgNotify)
			{
				MassMsgMgr.bRegisterMassMsgNotify = true;
				EventCenter.registerEventHandler(EventConst.ON_STORAGE_MASSMSG_ADD, new EventHandlerDelegate(MassMsgMgr.onMassMsgAdd));
				EventCenter.registerEventHandler(EventConst.ON_STORAGE_MASSMSG_MODIFY, new EventHandlerDelegate(MassMsgMgr.onMassMsgModify));
				EventCenter.registerEventHandler(EventConst.ON_STORAGE_MASSMSG_DEL, new EventHandlerDelegate(MassMsgMgr.onMassMsgDelete));
			}
		}
		private static void UnitMassMsgWatcher()
		{
			EventCenter.removeEventHandler(EventConst.ON_STORAGE_MASSMSG_ADD, new EventHandlerDelegate(MassMsgMgr.onMassMsgAdd));
			EventCenter.removeEventHandler(EventConst.ON_STORAGE_MASSMSG_MODIFY, new EventHandlerDelegate(MassMsgMgr.onMassMsgModify));
			EventCenter.removeEventHandler(EventConst.ON_STORAGE_MASSMSG_DEL, new EventHandlerDelegate(MassMsgMgr.onMassMsgDelete));
			MassMsgMgr.bRegisterMassMsgNotify = false;
		}
		private static void onMassMsgAdd(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<MassMsg> listObject = evtArgs.getListObject<MassMsg>();
			if (listObject == null)
			{
				return;
			}
			Log.smethod_5("MassMsgMgr", "onMassMsgAdd:" + listObject.get_Count());
			if (MassMsgMgr.MassMsgInfo != null && MassMsgMgr.MassMsgInfo.massMsgList != null)
			{
				MassMsgMgr.MassMsgInfo.msgListPage.hasMassMsgAddFlag = true;
				MassMsgMgr.AddNewMsgsToMsgList(MassMsgMgr.MassMsgInfo.massMsgList, listObject);
				MassMsgMgr.UpdateToConverSationList();
				return;
			}
		}
		private static void onMassMsgModify(EventWatcher watcher, BaseEventArgs evtArgs)
		{
		}
		private static void onMassMsgDelete(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<MassMsg> listObject = evtArgs.getListObject<MassMsg>();
			if (listObject == null)
			{
				return;
			}
			Log.smethod_5("MassMsgMgr", "onMassMsgDelete:" + listObject.get_Count());
			if (MassMsgMgr.MassMsgInfo != null && MassMsgMgr.MassMsgInfo.massMsgList != null)
			{
				MassMsgMgr.DelMsgsFromMsgList(MassMsgMgr.MassMsgInfo.massMsgList, listObject);
				MassMsgMgr.UpdateToConverSationList();
				return;
			}
		}
		private static void AddNewMsgsToMsgList(MMObservableCollection<MassMsg, MassSendListItem> massMsgList, List<MassMsg> listArgs)
		{
			for (int i = 0; i < listArgs.get_Count(); i++)
			{
				if (massMsgList.get_Count() == 0)
				{
					massMsgList.Insert(0, MassMsgMgr.GenerateTimeMsg(listArgs.get_Item(i)));
					massMsgList.Insert(1, listArgs.get_Item(i));
					MassMsgMgr.MassMsgInfo.msgListPage.showListView();
				}
				else
				{
					if (MassMsgMgr.IsNeedInsertTimeMsg(massMsgList[massMsgList.get_Count() - 1], listArgs.get_Item(i)))
					{
						massMsgList.Insert(massMsgList.get_Count(), MassMsgMgr.GenerateTimeMsg(listArgs.get_Item(i)));
					}
					massMsgList.Insert(massMsgList.get_Count(), listArgs.get_Item(i));
				}
			}
		}
		private static void DelMsgsFromMsgList(MMObservableCollection<MassMsg, MassSendListItem> massMsgList, List<MassMsg> listArgs)
		{
			int i = 0;
			IL_BA:
			while (i < listArgs.get_Count())
			{
				for (int j = 0; j < massMsgList.get_Count(); j++)
				{
					if (massMsgList[j] == listArgs.get_Item(i))
					{
						massMsgList.RemoveAt(j);
						if (j == massMsgList.get_Count())
						{
							if (j > 0 && massMsgList[j - 1] is MassSendTimeMsg)
							{
								massMsgList.RemoveAt(j - 1);
							}
						}
						else
						{
							if (!(massMsgList[j - 1] is MassSendTimeMsg) || !(massMsgList[j] is MassSendTimeMsg))
							{
								if (MassMsgMgr.IsNeedInsertTimeMsg(massMsgList[j - 1], massMsgList[j]))
								{
									massMsgList.Insert(j, MassMsgMgr.GenerateTimeMsg(massMsgList[j]));
									goto IL_7B;
								}
								j--;
								goto IL_7B;
							}
							else
							{
								massMsgList.RemoveAt(j - 1);
								j -= 2;
							}
						}
						IL_B6:
						i++;
						goto IL_BA;
					}
					IL_7B:;
				}
				goto IL_B6;
			}
			if (massMsgList.get_Count() == 0 && MassMsgMgr.MassMsgInfo.nLeftMsgTotal == 0)
			{
				MassMsgMgr.MassMsgInfo.msgListPage.showNoMsgView();
			}
		}
		public static void attachMsgData(MMObservableCollection<MassMsg, MassSendListItem> MassMsgList, MessagesListPage msgListPage)
		{
			if (MassMsgList == null || msgListPage == null)
			{
				return;
			}
			MassMsgMgr.InitMassMsgWatcher();
			ChatMsgMgr.registerCleanMsg(new Action(MassMsgMgr.MassMsgClearAll));
			if (MassMsgMgr.MassMsgInfo == null)
			{
				MassMsgMgr.MassMsgInfo = new MassMsgMgr.MassMsgContext();
			}
			MassMsgMgr.MassMsgInfo.msgListPage = msgListPage;
			MassMsgMgr.MassMsgInfo.massMsgList = MassMsgList;
			int num = StorageMgr.massMsg.count();
			int num2 = (num < 4) ? num : 4;
			if (num2 == 0)
			{
				return;
			}
			int num3 = num - num2;
			MassMsgMgr.MassMsgInfo.nLeftMsgTotal = num - num2;
			List<MassMsg> list = StorageMgr.massMsg.getList(num3, num2);
			if (list != null && list.get_Count() > 0)
			{
				MassMsgMgr.MassMsgInfo.toExpandList = StorageMgr.massMsg.getList(0, num - num2);
				for (int i = 0; i < num2; i++)
				{
					MassMsgList.Insert(i, list.get_Item(i));
				}
				MassMsgMgr.FillWithTimeMsg(MassMsgList);
				return;
			}
			Log.smethod_1("MassMsgMgr", "read mass msg error" + num3 + num2);
		}
		public static void detachMsgData()
		{
			MassMsgMgr.MassMsgInfo = null;
			MassMsgMgr.UnitMassMsgWatcher();
		}
		private static void FillWithTimeMsg(MMObservableCollection<MassMsg, MassSendListItem> MassMsgList)
		{
			MassMsgMgr.AddTimeAtListTop(MassMsgList);
			for (int i = 0; i < MassMsgList.get_Count() - 1; i++)
			{
				if (MassMsgMgr.IsNeedInsertTimeMsg(MassMsgList[i], MassMsgList[i + 1]))
				{
					MassMsgList.Insert(i + 1, MassMsgMgr.GenerateTimeMsg(MassMsgList[i + 1]));
					i++;
				}
			}
		}
		private static void AddTimeAtListTop(MMObservableCollection<MassMsg, MassSendListItem> MassMsgList)
		{
			if (MassMsgList == null || MassMsgList.get_Count() == 0)
			{
				return;
			}
			MassMsg massMsg = MassMsgList[0];
			if (massMsg is MassSendTimeMsg)
			{
				return;
			}
			MassSendTimeMsg massSendTimeMsg = MassMsgMgr.GenerateTimeMsg(massMsg);
			MassMsgList.Insert(0, massSendTimeMsg);
		}
		private static MassSendTimeMsg GenerateTimeMsg(MassMsg massMsg_0)
		{
			if (massMsg_0 == null)
			{
				return null;
			}
			return new MassSendTimeMsg
			{
				nTime = massMsg_0.nTime
			};
		}
		private static bool IsNeedInsertTimeMsg(MassMsg upMsg, MassMsg downMsg)
		{
			if (upMsg == null || downMsg == null)
			{
				return false;
			}
			if (!(upMsg is MassSendTimeMsg) && !(downMsg is MassSendTimeMsg))
			{
				long num = Math.Abs(upMsg.nTime - downMsg.nTime);
				return num >= 300L;
			}
			return false;
		}
		public static bool CanExpand()
		{
			return MassMsgMgr.MassMsgInfo != null && MassMsgMgr.MassMsgInfo.nLeftMsgTotal > 0;
		}
		public static int GetExpandNum()
		{
			return MassMsgMgr.MassMsgInfo.nLeftMsgTotal;
		}
		public static void ExpandMassMsgList()
		{
			if (MassMsgMgr.MassMsgInfo.nLeftMsgTotal <= 0)
			{
				return;
			}
			int num = (MassMsgMgr.MassMsgInfo.nLeftMsgTotal <= 4) ? MassMsgMgr.MassMsgInfo.nLeftMsgTotal : 4;
			for (int i = 0; i < num; i++)
			{
				MassMsgMgr.MassMsgInfo.massMsgList.Insert(0, MassMsgMgr.MassMsgInfo.toExpandList.get_Item(MassMsgMgr.MassMsgInfo.toExpandList.get_Count() - 1));
				MassMsgMgr.MassMsgInfo.toExpandList.RemoveAt(MassMsgMgr.MassMsgInfo.toExpandList.get_Count() - 1);
			}
			MassMsgMgr.MassMsgInfo.nLeftMsgTotal = MassMsgMgr.MassMsgInfo.toExpandList.get_Count();
			MassMsgMgr.FillWithTimeMsg(MassMsgMgr.MassMsgInfo.massMsgList);
		}
		public static void MassMsgClearAll()
		{
			MassMsgMgr.MassMsgClearCacheData();
			MassMsgMgr.MassMsgClearDBAndFile();
			Conversation conversation = StorageMgr.converation.method_0("masssend");
			if (conversation != null)
			{
				StorageMgr.converation.method_2(conversation);
			}
			if (MassMsgMgr.MassMsgInfo != null && MassMsgMgr.MassMsgInfo.msgListPage != null)
			{
				MassMsgMgr.MassMsgInfo.msgListPage.showNoMsgView();
			}
		}
		private static void MassMsgClearCacheData()
		{
			if (MassMsgMgr.MassMsgInfo != null)
			{
				MassMsgMgr.MassMsgInfo.nLeftMsgTotal = 0;
				if (MassMsgMgr.MassMsgInfo.massMsgList != null)
				{
					MassMsgMgr.MassMsgInfo.massMsgList.Clear();
				}
				if (MassMsgMgr.MassMsgInfo.toExpandList != null)
				{
					MassMsgMgr.MassMsgInfo.toExpandList.Clear();
				}
			}
		}
		private static void MassMsgClearDBAndFile()
		{
			StorageMgr.massMsg.clearAll();
			string pluginPath = StorageIO.getPluginPath(MassSend.getInstance().mName);
			StorageIO.emptyFile(pluginPath);
		}
		public static void UpdateToConverSationList()
		{
			Conversation conversation = StorageMgr.converation.method_0("masssend");
			if (conversation == null)
			{
				conversation = new Conversation();
				conversation.strUsrName = "masssend";
			}
			if (StorageMgr.massMsg.count() == 0)
			{
				conversation.strContent = "";
				conversation.nUnReadCount = 0;
				conversation.nIsSend = 0;
				conversation.strMsgType = 1.ToString();
				StorageMgr.converation.modify(conversation);
				return;
			}
			MassMsg massMsg;
			if (MassMsgMgr.MassMsgInfo.massMsgList.get_Count() != 0)
			{
				massMsg = MassMsgMgr.MassMsgInfo.massMsgList[MassMsgMgr.MassMsgInfo.massMsgList.get_Count() - 1];
			}
			else
			{
				massMsg = MassMsgMgr.MassMsgInfo.toExpandList.get_Item(MassMsgMgr.MassMsgInfo.nLeftMsgTotal - 1);
			}
			if (massMsg.nMsgType == 3u)
			{
				conversation.strContent = strings.ChatImgContent;
			}
			else
			{
				if (massMsg.nMsgType == 47u)
				{
					conversation.strContent = strings.ChatCustomSmileyContent;
				}
				else
				{
					if (massMsg.nMsgType == 34u)
					{
						conversation.strContent = strings.ChatVoiceContent;
					}
					else
					{
						if (massMsg.nMsgType == 1u)
						{
							conversation.strContent = massMsg.strContent;
							if (conversation.strContent != null && conversation.strContent.get_Length() >= 200)
							{
								conversation.strContent = conversation.strContent.Substring(0, 200);
							}
						}
					}
				}
			}
			conversation.strNickName = strings.Con_Plugin_MassSend_Nickname;
			conversation.strMsgType = 1.ToString();
			conversation.nIsSend = 0;
			conversation.nTime = massMsg.nTime;
			conversation.nUnReadCount = 0;
			StorageMgr.converation.update(conversation);
		}
	}
}
